/*
 * Text.java
 *
 * Created on 9 maj 2007, 19:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pl.krakow.ae.knp.wikiboard.gui.drawables;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import pl.krakow.ae.knp.wikiboard.gui.board.Board;
import pl.krakow.ae.knp.wikiboard.utils.GraphicalUtils;
/**
 *
 *
 */

public class Text extends DrawedObject
{
    private static final int PADDING = 2;
    private Point2D startPoint;
    private BufferedImage text;
    private Color color;
    
    /** Creates a new instance of Text.
     * @param startingPoint a point where the user clicked.
     * @param color current foreground color.
     * @param g Graphics context to draw to.
     * @param string a string to be drawed.
     */
     
    public Text(Point2D startingPoint, Color color, Graphics2D g, String string)
    {
        super();
        startPoint = startingPoint;
        this.color = new Color(color.getRGB());
        int height = g.getFontMetrics().getHeight();
        int width = g.getFontMetrics().stringWidth(string);
        text = new BufferedImage(width+PADDING, height+PADDING, BufferedImage.TYPE_INT_ARGB);
            Graphics2D textG = text.createGraphics();
            textG.setColor(color);
            textG.drawString(string, 0, height);
            textG.dispose();
    }
    
    protected Text(Text t)
    {
        super();
        this.startPoint = (Point2D)t.startPoint.clone();
        this.text = GraphicalUtils.cloneBufferedImage(t.text);
        this.color = GraphicalUtils.cloneColor(t.color);
    }
    
    public void draw(Graphics2D g, boolean dragged)
    {
        g.setXORMode(Board.getBgColor());
        if(!dragged)
            g.setPaintMode();
        g.setColor(color);
        g.drawImage(text, (int)startPoint.getX(), (int)startPoint.getY(), null);
        
        if (isSelected)
        {
            Graphics2D g2 = (Graphics2D)g.create();
            g2.setColor(Board.getSelectColor());
            g2.setStroke(new BasicStroke(STROKE_WIDTH));
            g2.drawRect( (int)startPoint.getX() - SELECT_PADDING,
                    (int)startPoint.getY() - SELECT_PADDING,
                    (int)text.getWidth() + 2*SELECT_PADDING,
                    (int)text.getHeight() + 2*SELECT_PADDING);
            g2.dispose();
        }
    }
    
    public void move(double x, double y)
    { startPoint.setLocation(startPoint.getX()+x, startPoint.getY()+y); }
    
    public void rotate()
    {
    }
    
    public void drag(MouseEvent event)
    {}
    
    public boolean contains(Point2D p)
    {
        return ( (p.getX() >= startPoint.getX())
        &&     (p.getX() < (startPoint.getX() + text.getWidth()))
        &&     (p.getY() >= startPoint.getY())
        &&     (p.getY() < (startPoint.getY() + text.getHeight())) );
    }
    
    public DrawedObject clone()
    { return new Text(this); }
    
    
}
